/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.internalClasses;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.utils.GdxRuntimeException;

import com.jdiv.JDiv;
import com.jdiv.kernel.JDivSystem;

/** This is texture for JDIV */

public class JDivTexture extends JDivDisposable
{
 private FileHandle file;
 private boolean isAtlas;
 private Texture texture;
 private Pixmap  pixmap;
 private Pixmap  texturePixmap;
 
 private TextureAtlas textureAtlas;
 
 //-----------------------------------------------------------------------------
 // JDivTexture: Inicializamos la textura
 //-----------------------------------------------------------------------------

 public JDivTexture (Pixmap texturePixmap) {

	 this.file    = null;
	 this.isAtlas = false;
	 this.texture = null;
	 this.texturePixmap = texturePixmap;
	 pixmap  = null;
	 textureAtlas = null;

 }

 public JDivTexture (Texture texture) {

	 this.file    = null;
	 this.isAtlas = false;
	 this.texture = texture;
	 pixmap  = null;
	 textureAtlas = null;

 }
 
 public JDivTexture( FileHandle file, boolean isAtlas )
 {
	 this.file    = file;
	 this.isAtlas = isAtlas;
	 texture = null;
	 pixmap  = null;
	 textureAtlas = null;
 }
 
 //-----------------------------------------------------------------------------
 // CreateTexture: Creamos la textura
 //-----------------------------------------------------------------------------

 public void load(){
	 if( !isAtlas ){
		  try{
			  if (file != null)
				  texture = new Texture( file );
			  else if (texturePixmap != null)
				  texture = new Texture(texturePixmap);
		  }catch( GdxRuntimeException e ){
		   texture = null;
		   JDiv.system.log("ERROR, Not CreateTexture: " + e.getMessage());
		   return;
		  }
		  
		  if( texture != null )
		   texture.setFilter(JDivSystem.jdivTextureFilter, JDivSystem.jdivTextureFilter); 
	  }else
		  textureAtlas = new TextureAtlas( file );
 }
 
 //-----------------------------------------------------------------------------
 // setTiled: Establece el modo repetitivo
 //-----------------------------------------------------------------------------
 
 public void setTiled(){
	 if( texture != null ){
		 texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
	 }
 }
 
//-----------------------------------------------------------------------------
// disableTiled: Desactiva el modo repetitivo
//-----------------------------------------------------------------------------
 
 public void disableTiled(){
	 if( texture != null )
		 texture.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
 }
 
 //-----------------------------------------------------------------------------
 // getPixel: Obtenemos un pixel de una textura
 //-----------------------------------------------------------------------------
 
 public int getPixel(int x, int y){
	 
	 if( (pixmap == null) && file.exists() )
		 pixmap = new Pixmap( file );
	 
	 if( (pixmap == null) || (x < 0) || (y < 0) ||
		 (x >= pixmap.getWidth()) || (y >= pixmap.getHeight()) ){
		 
		 if( JDivSystem.debugMode )
		  JDiv.system.log("ERROR, getPixel() - Pixmap error from: " + file.path());
		 
		 return -1;
	 }
	 
	 return pixmap.getPixel(x, y);
 }
 
 //-----------------------------------------------------------------------------
 // getImage: Obtenemos los datos de la imagen
 //-----------------------------------------------------------------------------
 
 void getImageData( JDivImage jdivImage ){
    //Comprobamos si es una imagen que viene de un atlas
	if( (jdivImage.stringAtlas.length() > 0) && (textureAtlas != null) )
	{
	 AtlasRegion atlasRegion = textureAtlas.findRegion(jdivImage.stringAtlas);
				
	 // Si no existe la region
	 if( atlasRegion == null ){
		 JDiv.system.log("ERROR, JDivTexture.getImageData() - File not found: " +  jdivImage.stringAtlas );	
		 return;
	 }
				
	 // Si no hemos cargado las coordenadas la cargamos
	 if(jdivImage.width == 0){
		 jdivImage.x      = atlasRegion.getRegionX();
		 jdivImage.y      = atlasRegion.getRegionY();
		 jdivImage.width  = atlasRegion.getRegionWidth();
		 jdivImage.height = atlasRegion.getRegionHeight();
	 }
	}
 }
 
 //-----------------------------------------------------------------------------
 // getImage: Obtenemos la imagen de la textura
 //-----------------------------------------------------------------------------
 
 public TextureRegion getTextureRegion( JDivImage jdivImage ){
	 
	    if( (textureAtlas == null) && (texture == null) )
			return null;
	 
	    // Comprobamos si es una imagen que viene de un atlas
		if( (jdivImage.stringAtlas.length() > 0) && (textureAtlas != null) )
		{
		 AtlasRegion atlasRegion = textureAtlas.findRegion(jdivImage.stringAtlas);
					
		 // Si no existe la region
		 if( atlasRegion == null ){		 
			 JDiv.system.log("ERROR, textureAtlas.findRegion() - File not found: " +  jdivImage.stringAtlas );	
			 return null;
		 }
		 
		 return atlasRegion;
		}
		
		// Si viene de una textura
		if( (jdivImage.stringAtlas.length() == 0) && ( texture != null ) )
		{
		 TextureRegion textureRegion = new TextureRegion(texture, 
				 jdivImage.x, 
				 jdivImage.y,
				 jdivImage.width,
				 jdivImage.height);
		 
		 return textureRegion;
		}
		
		return null;
	 
 }
 
 //-----------------------------------------------------------------------------
 // setTexture: Establecemos la textura
 //-----------------------------------------------------------------------------

 public void setTexture( Texture texture ){
	 this.texture = texture;
	 
	 if( textureAtlas != null ){
		 textureAtlas.dispose();
		 textureAtlas = null;
	 }
 }
 
 //-----------------------------------------------------------------------------
 // getTexture: Obtenemos la textura
 //-----------------------------------------------------------------------------

 public Texture getTexture( ){
	 return texture;
 }

 //-----------------------------------------------------------------------------
 // setTexture: Establecemos la textura Atlas
 //-----------------------------------------------------------------------------
	
 public void setTexture( TextureAtlas texture ){
	 textureAtlas = texture;
	 
	 if( this.texture != null ){
		  this.texture.dispose();
		  this.texture = null;
	 }
 }
 
//-----------------------------------------------------------------------------
// disablePixmap: Liberamos el Pixmap
//-----------------------------------------------------------------------------
 
 public void disablePixmap(){
	 if( pixmap != null ){
		 pixmap.dispose();
		 pixmap = null;
	 }
 }
 
 //-----------------------------------------------------------------------------
 // dispose: Liberamos las texturas
 //-----------------------------------------------------------------------------
	
 public void dispose(){
	 if( texture != null ){
		 texture.dispose();
		 texture = null;
	 }
	 
	 if( pixmap != null ){
		 pixmap.dispose();
		 pixmap = null;
	 }
	 
	 if( textureAtlas != null ){
		 textureAtlas.dispose();
		 textureAtlas = null;
	 }
 }

 
}


